package models

import (
	"fly/schema"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	"time"
)

type BizConfig struct {
	BaseModel
	Name        string    `orm:"column(name);description(名称)"`
	Description string    `orm:"column(description);description(描述)"`
	Key         string    `orm:"column(key);description(key)"`
	Type        int       `orm:"column(type);description(类型 1次数配置 2开关配置)"`
	Value       int       `orm:"column(value);description(配置的值,开关只能1和0 分别表示开和关)"`
	Enabled     int       `orm:"column(enabled);description(启用状态,1:启用，0:停用)"`
	ExpireTime  time.Time `orm:"column(expire_time);description(过期时间)"`
}

func init() {
	orm.RegisterModel(new(BizConfig))
}

// SearchField 定义模型的可搜索字段
func (*BizConfig) SearchField() []string {
	return []string{"name", "key", "type", "enabled"}
}

// WhereField 定义模型可作为条件的字段
func (*BizConfig) WhereField() []string {
	return []string{"id", "name", "key", "type", "enabled"}
}

func (m *BizConfig) TableName() string {
	return "biz_config"
}

// AddBizConfig insert a new BizConfig into database and returns
// last inserted Id on success.
func AddBizConfig(m *BizConfig, userId int64) (id int64, err error) {
	o := orm.NewOrm()
	m.Creator = userId
	m.Updater = userId
	id, err = o.Insert(m)
	return
}

// GetBizConfigById retrieves BizConfig by Id. Returns error if
// Id doesn't exist
func GetBizConfigById(id int64) (v *BizConfig, err error) {
	o := orm.NewOrm()
	v = &BizConfig{BaseModel: BaseModel{Id: id}}
	if err = o.QueryTable(new(BizConfig)).Filter("id", id).RelatedSel().One(v); err == nil {
		return v, nil
	}
	return nil, err
}

// UpdateBizConfigById updates BizConfig by Id and returns error if
// the record to be updated doesn't exist
func UpdateBizConfigById(m *BizConfig, userId int64) (err error) {
	o := orm.NewOrm()
	v := BizConfig{BaseModel: BaseModel{Id: m.Id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		m.Updater = userId
		var num int64
		if num, err = o.Update(m); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}

// DeleteBizConfig deletes BizConfig by Id and returns error if
// the record to be deleted doesn't exist
func DeleteBizConfig(id int64) (err error) {
	o := orm.NewOrm()
	v := BizConfig{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		var num int64
		if num, err = o.Delete(&BizConfig{BaseModel: BaseModel{Id: id}}); err == nil {
			fmt.Println("Number of records deleted in database:", num)
		}
	}
	return
}

// DestroyBizConfig Destroy BizConfig by Id and returns error if
func DestroyBizConfig(id int64, userId int64) (err error) {
	o := orm.NewOrm()
	v := BizConfig{BaseModel: BaseModel{Id: id}}
	// ascertain id exists in the database
	if err = o.Read(&v); err == nil {
		v.Status = schema.StatusDelete
		v.Updater = userId
		var num int64
		if num, err = o.Update(v, "updater", "status"); err == nil {
			fmt.Println("Number of records updated in database:", num)
		}
	}
	return
}
